<!-- ContactList.vue -->
<template>
  <div class="contact-list">
    <div class="contact-list-header">
      <h3>联系人列表</h3>
    </div>
    <div class="contact-items">
      <div
        v-for="contact in contacts"
        :key="contact.contactUserId"
        :class="['contact-item', { active: selectedContact && selectedContact.contactUserId === contact.contactUserId }]"
        @click="selectContact(contact)"
      >
        <div class="avatar">
          <img :src="contact.avatarPath" :alt="contact.contactName" />
          <div :class="['status-indicator', { online: contact.status === 1 }]"></div>
        </div>
        <div class="contact-info">
          <div class="contact-name">{{ contact.contactName }}</div>
          <div class="contact-status">{{ contact.status === 1 ? '在线' : '离线' }}</div>
        </div>
        <div v-if="contact.unreadMessageCount > 0" class="unread-badge">
          {{ contact.unreadMessageCount > 99 ? '99+' : contact.unreadMessageCount }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ContactList',
  props: {
    contacts: {
      type: Array,
      required: true
    },
    selectedContact: {
      type: Object,
      default: null
    }
  },
  methods: {
    selectContact (contact) {
      this.$emit('contact-selected', contact)
    }
  }
}
</script>

<style scoped>
.contact-list {
  width: 100%;
  height: 100%;
  background-color: #f8f9fa;
  border-right: 1px solid #e0e0e0;
  display: flex;
  flex-direction: column;
}

.contact-list-header {
  padding: 16px;
  border-bottom: 1px solid #e0e0e0;
  background-color: #ffffff;
}

.contact-list-header h3 {
  margin: 0;
  color: #1976d2;
}

.contact-items {
  flex: 1;
  overflow-y: auto;
  /* 确保滚动条可见 */
  scrollbar-width: thin;
  scrollbar-color: #1976d2 #e0e0e0;
}

/* Webkit浏览器滚动条样式 */
.contact-items::-webkit-scrollbar {
  width: 8px;
}

.contact-items::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.contact-items::-webkit-scrollbar-thumb {
  background-color: #1976d2;
  border-radius: 4px;
  border: 2px solid #f1f1f1;
}

.contact-items::-webkit-scrollbar-thumb:hover {
  background-color: #1565c0;
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.2s;
  border-bottom: 1px solid #eee;
  position: relative;
}

.contact-item:hover {
  background-color: #e3f2fd;
}

.contact-item.active {
  background-color: #bbdefb;
}

.avatar {
  position: relative;
  margin-right: 12px;
}

.avatar img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
}

.status-indicator {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #9e9e9e;
  border: 2px solid #ffffff;
}

.status-indicator.online {
  background-color: #4caf50;
}

.contact-info {
  flex: 1;
}

.contact-name {
  font-weight: 500;
  color: #333;
}

.contact-status {
  font-size: 12px;
  color: #757575;
}

.unread-badge {
  background-color: #f44336;
  color: white;
  border-radius: 10px;
  padding: 2px 6px;
  font-size: 12px;
  font-weight: bold;
  min-width: 20px;
  text-align: center;
}
</style>
